
#!/bin/bash

CURRENT_DIRECTORY=`pwd`
export LIB_ROOT_DIR=${CURRENT_DIRECTORY}/libs
export INSTALL_DIR=${LIB_ROOT_DIR}/linux
export SOURCE_DIR=${LIB_ROOT_DIR}/sources_linux
export BUILD_DIR=${LIB_ROOT_DIR}/build_linux

rm -fr ${BUILD_DIR}
mkdir -p ${LIB_ROOT_DIR}
mkdir -p ${INSTALL_DIR}
mkdir -p ${SOURCE_DIR}
mkdir -p ${BUILD_DIR}

echo ${CURRENT_DIRECTORY}

# =================================Install SDL2===================================
LIB_VERSION=2.0.22
LIB_NAME=SDL2-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/libsdl-org/SDL/releases/download/release-${LIB_VERSION}/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}
	#make clean
    ./configure --with-pic --prefix=${LIB_INSTALL_DIR}
    make
    make install
    cd -
fi

# =================================Install glad===================================
LIB_VERSION=0.1.36
LIB_NAME=glad-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	if [ ! -d ${LIB_SRC_DIR} ]; then
		if [ -f ${LIB_SRC_TAR_GZ} ]; then
			tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		else
            wget https://github.com/Dav1dde/glad/archive/refs/tags/v${LIB_VERSION}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
		fi
	fi

    mkdir -p ${LIB_BUILD_DIR}
    cmake -B ${LIB_BUILD_DIR} -S ${LIB_SRC_DIR} -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
    cmake --build ${LIB_BUILD_DIR}
    cmake --install ${LIB_BUILD_DIR}
fi
